package org.yagnus.scalasupport

object RichArrays {

   def compareArrays[A <% Ordered[A]](ll : Array[A], rr : Array[A]) : Int = {
      if (ll.length == rr.length) {
         for (i ← 1 until ll.length) {
            val tl : A = ll(i);
            val tr : A = rr(i);

            val cr = tl.compare(tr);
            if (cr != 0) return cr;

         }
         return 0;
      } else {
         if (ll.length > rr.length) return 1;
         else return -1;
      }
   }

   class ComparableArray[A <% Ordered[A]](in : Array[A]) extends Ordered[Array[A]] {
      def toArray = in;
      override def compare(o : Array[A]) : Int = compareArrays(in, o);
      override def compareTo(o : Array[A]) : Int = compareArrays(in, o);
      override def equals(o : Any) : Boolean = {
         o match {
            case t : ComparableArray[A] ⇒ return this.compareTo(t) == 0;
            case _ ⇒ false;
         }

      }
   }


   implicit def makeArrayComparable[A <% Ordered[A]](inArray : Array[A]) : ComparableArray[A] = new ComparableArray(inArray);
   implicit def recoverArray[A <% Ordered[A]](in : ComparableArray[A]) : Array[A] = in.toArray;

}